home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / parse.y < prev    next >
Text File  |  1997-12-07  |  36KB  |  1,433 lines

  1. %{
  2. /***************************************
  3.   $Header: /home/amb/cxref/RCS/parse.y 1.29 1997/11/20 19:19:07 amb Exp $
  4.  
  5.   C Cross Referencing & Documentation tool. Version 1.4a.
  6.  
  7.   C parser.
  8.   ******************/ /******************
  9.   Written by Andrew M. Bishop
  10.  
  11.   This file Copyright 1995,96,97 Andrew M. Bishop
  12.   It may be distributed under the GNU Public License, version 2, or
  13.   any higher version.  See section COPYING of the GNU Public license
  14.   for conditions under which this file may be redistributed.
  15.   ***************************************/
  16.  
  17. #include <string.h>
  18. #include "parse-yy.h"
  19. #include "cxref.h"
  20. #include "memory.h"
  21.  
  22. /*+ A structure to hold the information about an object. +*/
  23. typedef struct _stack
  24. {
  25.  char *name;                    /*+ The name of the object. +*/
  26.  char *type;                    /*+ The type of the object. +*/
  27.  char *qual;                    /*+ The type qualifier of the object. +*/
  28. }
  29. stack;
  30.  
  31. #define yylex cxref_yylex
  32.  
  33. static int cxref_yylex(void);
  34.  
  35. static void yyerror(char *s);
  36.  
  37. /*+ When in a header file, some stuff can be skipped over quickly. +*/
  38. extern int in_header;
  39.  
  40. /*+ A flag that is set to true when typedef is seen in a statement. +*/
  41. int in_typedef=0;
  42.  
  43. /*+ The scope of the function / variable that is being examined. +*/
  44. static int scope;
  45.  
  46. /*+ The variable must be LOCAL or EXTERNAL or GLOBAL, so this checks and sets that. +*/
  47. #define SCOPE ( scope&(LOCAL|EXTERNAL|EXTERN_H|EXTERN_F) ? scope : scope|GLOBAL )
  48.  
  49. /*+ When in a function or a function definition, the behaviour is different. +*/
  50. static int in_function=0,in_funcdef=0,in_funcbody=0;
  51.  
  52. /*+ The parsing stack +*/
  53. static stack first={NULL,NULL,NULL},  /*+ first value. +*/
  54.             *list=NULL,               /*+ list of all values. +*/
  55.             *current=&first;          /*+ current values. +*/
  56.  
  57. /*+ The depth of the stack +*/
  58. static int depth=0,             /*+ currently in use. +*/
  59.            maxdepth=0;          /*+ total malloced. +*/
  60.  
  61. /*+ Declarations that are in the same statement share this comment. +*/
  62. static char* common_comment=NULL;
  63.  
  64. /*+ When inside a struct / union / enum definition, this is the depth. +*/
  65. static int in_structunion=0;
  66.  
  67. /*+ When inside a struct / union definition, this is the component type. +*/
  68. static char *comp_type=NULL;
  69.  
  70. /*+ To solve the problem where a type name is used as an identifier. +*/
  71. static int in_type_spec=0;
  72.  
  73.  
  74. /*++++++++++++++++++++++++++++++++++++++
  75.   Reset the current level on the stack.
  76.   ++++++++++++++++++++++++++++++++++++++*/
  77.  
  78. static void reset(void)
  79. {
  80.  current->name=NULL;
  81.  current->type=NULL;
  82.  current->qual=NULL;
  83. }
  84.  
  85.  
  86. /*++++++++++++++++++++++++++++++++++++++
  87.   Push a level onto the stack.
  88.   ++++++++++++++++++++++++++++++++++++++*/
  89.  
  90. static void push(void)
  91. {
  92.  if(list==NULL)
  93.    {
  94.     list=(stack*)Malloc(8*sizeof(struct _stack));
  95.     list[0]=first;
  96.     maxdepth=8;
  97.    }
  98.  else if(depth==maxdepth)
  99.    {
  100.     list=Realloc(list,(maxdepth+8)*sizeof(struct _stack));
  101.     maxdepth+=8;
  102.    }
  103.  
  104.  depth++;
  105.  current=&list[depth];
  106.  
  107.  reset();
  108. }
  109.  
  110.  
  111. /*++++++++++++++++++++++++++++++++++++++
  112.   Pop a level from the stack.
  113.   ++++++++++++++++++++++++++++++++++++++*/
  114.  
  115. static void pop(void)
  116. {
  117.  reset();
  118.  
  119.  depth--;
  120.  current=&list[depth];
  121. }
  122.  
  123.  
  124. /*++++++++++++++++++++++++++++++++++++++
  125.   Reset the Parser, ready for the next file.
  126.   ++++++++++++++++++++++++++++++++++++++*/
  127.  
  128. void ResetParser(void)
  129. {
  130.  in_typedef=0;
  131.  scope=0;
  132.  in_function=0;
  133.  in_funcdef=0;
  134.  in_funcbody=0;
  135.  depth=0;
  136.  maxdepth=0;
  137.  if(list) Free(list);
  138.  list=NULL;
  139.  current=&first;
  140.  reset();
  141.  common_comment=NULL;
  142.  in_structunion=0;
  143.  comp_type=NULL;
  144.  in_type_spec=0;
  145. }
  146.  
  147. %}
  148.  
  149. /* Expected conflicts: 17 shift/reduce */
  150.  
  151. %token IDENTIFIER TYPE_NAME LITERAL STRING_LITERAL ELLIPSES
  152. %token MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
  153. %token EQ_OP NE_OP PTR_OP AND_OP OR_OP DEC_OP INC_OP LE_OP GE_OP
  154. %token LEFT_SHIFT RIGHT_SHIFT
  155. %token SIZEOF
  156. %token TYPEDEF EXTERN STATIC AUTO REGISTER CONST VOLATILE VOID INLINE
  157. %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE
  158. %token STRUCT UNION ENUM
  159. %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
  160. %token ASM
  161.  
  162. %start file
  163.  
  164. %%
  165.  
  166. /*-------------------- Top level --------------------*/
  167.  
  168. file
  169.     : /* Empty */
  170.     | program
  171.     ;
  172.  
  173. program
  174.     : top_level_declaration
  175.     | program top_level_declaration
  176.     ;
  177.  
  178. top_level_declaration
  179.     : declaration
  180.                 { scope=0; reset(); common_comment=NULL; in_typedef=0; }
  181.     | function_definition
  182.                 { scope=0; reset(); common_comment=NULL; in_typedef=0; }
  183.     | asm_statement         /*+ GNU Extension +*/
  184.     | null_statement
  185.     ;
  186.  
  187. /*-------------------- Declarations --------------------*/
  188.  
  189. declaration_list
  190.     : declaration
  191.                 { scope=0; reset(); common_comment=NULL; in_typedef=0; }
  192.     | declaration_list declaration
  193.                 { scope=0; reset(); common_comment=NULL; in_typedef=0;
  194.                   $$=$2; }
  195.     ;
  196.  
  197. declaration
  198.     : declaration_specifiers initialized_declarator_list ';'
  199.                 { in_type_spec=0; }
  200.     | declaration_specifiers ';'
  201.                 { in_type_spec=0; }
  202.     ;
  203.  
  204. declaration_specifiers
  205.         : declaration_specifiers1
  206.                 { if(!in_typedef) {common_comment=GetCurrentComment(); SetCurrentComment(common_comment);} }
  207.     ;
  208.  
  209. declaration_specifiers1
  210.     : storage_class_specifier
  211.     | storage_class_specifier declaration_specifiers1
  212.                 { if($1) $$=ConcatStrings(3,$1," ",$2); else $$=$2; }
  213.     | type_specifier
  214.                 { if(!current->type) current->type=$1; }
  215.     | type_specifier declaration_specifiers1
  216.                 { if(!current->type) current->type=$1;
  217.                   $$=ConcatStrings(3,$1," ",$2); }
  218.     | type_qualifier
  219.     | type_qualifier declaration_specifiers1
  220.                 { $$=ConcatStrings(3,$1," ",$2); }
  221.     ;
  222.  
  223. /* Initialised declarator list */
  224.  
  225. initialized_declarator_list
  226.     : initialized_declarator
  227.     | initialized_declarator_list ',' { in_type_spec=1; } initialized_declarator
  228.     ;
  229.  
  230. initialized_declarator
  231.     : initialized_declarator1
  232.                 {
  233.                  if(!in_function && !in_funcdef && !in_structunion)
  234.                    {
  235.                     char* specific_comment=GetCurrentComment();
  236.                     if(!common_comment)   SetCurrentComment(specific_comment); else
  237.                     if(!specific_comment) SetCurrentComment(common_comment);   else
  238.                     if(common_comment!=specific_comment) SetCurrentComment(ConcatStrings(3,common_comment," ",specific_comment)); else
  239.                                           SetCurrentComment(common_comment);
  240.                    }
  241.  
  242.                  if(in_typedef)
  243.                    {
  244.                     char* vname=strstr($1,current->name);
  245.                     SeenTypedefName(current->name,vname[strlen(current->name)]=='('?-1:1);
  246.                     if(!in_header)
  247.                        SeenTypedef(current->name,ConcatStrings(3,current->qual,current->type,$1));
  248.                    }
  249.                  else
  250.                     if(in_function==2)
  251.                        SeenFunctionArg(current->name,ConcatStrings(3,current->qual,current->type,$1));
  252.                     else
  253.                       {
  254.                        char* vname=strstr($1,current->name);
  255.                        if(vname[strlen(current->name)]!='(' && IsATypeName(current->type)!='f')
  256.                          {
  257.                           if((in_funcbody==0 || scope&EXTERN_F) && !in_structunion && !(in_header==GLOBAL && scope&EXTERN_H))
  258.                              SeenVariableDefinition(current->name,ConcatStrings(3,current->qual,current->type,$1),SCOPE);
  259.                           else
  260.                              if(in_funcbody)
  261.                                 SeenScopeVariable(current->name);
  262.                          }
  263.                        else
  264.                          {
  265.                           SeenFunctionProto(current->name,in_funcbody);
  266.                           DownScope();
  267.                          }
  268.                       }
  269.  
  270.                  if(in_function==3) in_function=0;
  271.                 }
  272.     ;
  273.  
  274. initialized_declarator1
  275.     : declarator
  276.     | declarator asm_label  /*+ GNU Extension +*/
  277.     | declarator initializer_part
  278.     | declarator asm_label initializer_part /* GNU Ext